import processing.core.*; 
import processing.xml.*; 

import processing.opengl.*; 
import codeanticode.glgraphics.*; 
import deadpixel.keystone.*; 

import java.applet.*; 
import java.awt.Dimension; 
import java.awt.Frame; 
import java.awt.event.MouseEvent; 
import java.awt.event.KeyEvent; 
import java.awt.event.FocusEvent; 
import java.awt.Image; 
import java.io.*; 
import java.net.*; 
import java.text.*; 
import java.util.*; 
import java.util.zip.*; 
import java.util.regex.*; 

public class CornerPinOpenGL2 extends PApplet {

/**
 * This is another example of a keystoned surface, this time using Andres
 * Colubri's GLGraphics library. The library has an offscreen renderer that
 * will draw your sketch to an OpenGL texture.
 *
 * To run this example, you'll need the GLGraphics library. Get it here:
 * http://users.design.ucla.edu/~acolubri/processing/glgraphics/home/ 
 */






// this object is key! you can use it to render fully accelerated
// OpenGL scenes directly to a texture
GLGraphicsOffScreen[] offscreen;

Keystone[] ks;

CornerPinSurface[] surface;

public void setup() {
  size(1024, 768, GLConstants.GLGRAPHICS);

  offscreen = new GLGraphicsOffScreen[2]; 
  
  offscreen[0] = new GLGraphicsOffScreen(this, 320, 240, true, 4);
  offscreen[1] = new GLGraphicsOffScreen(this, 200, 100);


  ks = new Keystone[2];
   surface = new CornerPinSurface[2]; 
  
  ks[0]= new Keystone(this);
    ks[1]= new Keystone(this);
    
    surface[0] = ks[0].createCornerPinSurface(320, 240, 20);
    surface[1] = ks[1].createCornerPinSurface(200, 100, 20);
}

public void draw() {
  // convert 
  background(0);
  // first draw the sketch offscreen
  drawSurface(offscreen[0],surface[0]);
  drawSurface(offscreen[1],surface[1]);

  // then render the sketch using the 
  // keystoned surface
  


}

public void drawSurface(GLGraphicsOffScreen offs,CornerPinSurface surf){
  PVector mouse = surf.getTransformedMouse();
  offs.beginDraw();
  offs.background(50);
  offs.lights();
  offs.fill(255);
  offs.translate(mouse.x, mouse.y);
  offs.rotateX(millis()/200.0f);
  offs.rotateY(millis()/400.0f);
  offs.box(100);
  offs.endDraw(); 
    surf.render(offs.getTexture());
}

public void keyPressedOwn(Keystone ks){
  switch(key) {
  case 'c':
    // enter/leave calibration mode, where surfaces can be warped 
    // & moved
    ks.toggleCalibration();
    break;

  case 'l':
    // loads the saved layout
    ks.load();
    break;

  case 's':
    // saves the layout
    ks.save();
    break;
  }
}

Keystone ksSelected = null;

// Controls for the Keystone object
public void keyPressed() {

  if(key == '1'){
      ksSelected = ks[1];
  }else   if(key == '0'){
      ksSelected = ks[0];
  }
  
  if(ksSelected != null){
    keyPressedOwn(ksSelected);    
  }
  
}
  static public void main(String args[]) {
    PApplet.main(new String[] { "--bgcolor=#F0F0F0", "CornerPinOpenGL2" });
  }
}
